#!/usr/bin/expect -f

set +e
if [ -z "$1"  ]
then
  echo "请设置起始值！"
  exit 1;
fi

if [ -z "$2"   ]
then
  echo "截止到第几个...？"
  exit 1;
fi

source=''
if [ -z "$3"   ];then
  source='lotus'
else
  source=$3
fi



echo $1-$2

user='ceshi'
IPPrefix="192.168.8."
curPath=$(dirname $(readlink -f "$0"))
PASSWD=$(cat $curPath/../.env)
# source='dev_sched'

commandMV='sudo mv ~/lotus-worker /usr/local/bin/lotus-worker'
command='/usr/local/bin/lotus-worker --version'


for((i=$1;i<$2;i++));
# for i in {101..116} 
do

  echo "------link--> $IPPrefix$i---------"

  # if(($i==14 || $i==34 || $i==2 || $i==200)); then
	# 	echo "跳过..."
	# 	continue
	# fi

expect << EOF
  set time 30
  # spawn scp /home/ceshi/code/$source/lotus-worker $user@$IPPrefix$i:/usr/local/bin/lotus-worker
  spawn scp /home/ceshi/code/$source/lotus-worker $user@$IPPrefix$i:~
  
  expect {
    "*yes/no" { send "yes\r";}
    "*assword:" { send "$PASSWD\r"; exp_continue}
   
  }
  
EOF
#ConnectTimeout=3                   连接超时时间，3秒
#ConnectionAttempts=5               连接失败后重试次数，5次
#PasswordAuthentication=no          不使用密码认证,没有互信直接退出
#StrictHostKeyChecking=no           自动信任主机并添加到known_hosts文件

  sshpass -p $PASSWD ssh -o ConnectTimeout=4 -o ConnectionAttempts=5 $user@$IPPrefix$i "$commandMV"
  sshpass -p $PASSWD ssh -o ConnectTimeout=4 -o ConnectionAttempts=5  $user@$IPPrefix$i "$command"

  # sshpass -p $PASSWD ssh  $user@$IPPrefix$i "$commandMV"
  # sshpass -p $PASSWD ssh  $user@$IPPrefix$i "$command"

  echo "--------------------------------"
  echo ""

  if [ -z "$4"   ]
  then
    sleep 1
  fi
done





# sudo chmod 777 scpTest.sh

